package com.gouwudai.table.tag;

import java.util.StringTokenizer;

import javax.servlet.jsp.JspException;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.tag.ColumnTag;
import org.extremecomponents.table.tag.TagUtils;

/**
 * <TITLE>SwitchColumnTag - 增强型表格列输出</TITLE>
 * <P>defaultValues - 默认值数组，例如： 1,2,3,4</P>
 * <P>displays - 对应显示文字，例如：A,B,C,D</P>
 * <P>当有具体的内容体的时候也可以用 || 将不同的内容体分开，一个默认值对应一个内容，例如：A||B||C||D</P>
 * <P>PS:属性的内容不能为BigDecimal型</P>
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2006-10-20
 * @version 1.0
 *
 */
public class SwitchColumnTag extends ColumnTag {
	
	private String defaultValues;	//默认值
	private String displays;		//值对应显示文字
	private String styleClasses;		//样式表
	
	private String[] valueArray;
	private String[] displayArray;
	private String[] styleArray;
	/**
	 * 
	 */	
	private static final long serialVersionUID = -8805331511452677029L;
	
	
	public void addColumnAttributes(TableModel tableModel, Column column) {		
		column.addAttribute("defaultValues", this.defaultValues);
		column.addAttribute("displays", this.displays);
		column.addAttribute("styleClasses", this.styleClasses);			
	}
	
	

	
	@Override
	public int doStartTag() throws JspException {
		valueArray = defaultValues.split(",");
		if(displays!=null)
		{
			displayArray = displays.split(",");			
		}
		if(styleClasses!=null)
		{
			styleArray = styleClasses.split(",");			
		}
		return super.doStartTag();		
	}




	public void modifyColumnAttributes(TableModel tableModel, Column column) {
		column.addAttribute("defaultValues", this.defaultValues);
		column.addAttribute("displays", this.displays);
		column.addAttribute("styleClasses", this.styleClasses);
	}

	
	protected Object getColumnValue(Object propertyValue) throws JspException {		
		//如果标签体中有内容，优先处理 内容		
		if(bodyContent!=null)
		{			
			String body = ExpressionEvaluatorManager.evaluate("result", getBodyContent().getString(), Object.class, this, pageContext).toString();
			StringTokenizer spitToker = new StringTokenizer(body, "||");	
			int count = spitToker.countTokens();
			displayArray = new String[count];			
			int i=0;
			while(spitToker.hasMoreTokens())
			{
				displayArray[i] = spitToker.nextToken();					
				i++;
			}
		}		
		if(propertyValue!=null)
		{			
			for(int j=0;j<valueArray.length;j++)
			{
				if(valueArray[j].equals(propertyValue.toString()))	
				{	
					//设置对应样式
					if(styleArray!=null&&styleArray[j]!=null&&!styleArray[j].trim().equals(""))
						super.setStyleClass(styleArray[j]);
					return displayArray[j].trim();
				}	
			}			
		}
		return "<font color=\"red\">无匹配</font>";
	}	

	
	public void release() {		
		super.release();
		this.defaultValues = null;
		this.displays = null;
		this.styleClasses = null;
		this.valueArray = null;
		this.displayArray = null;
		this.styleArray = null;
	}	

	public String getDefaultValues() {
		return defaultValues;
	}

	public void setDefaultValues(String defaultValues) {
		this.defaultValues = defaultValues;
	}

	public String getDisplays() {
		return displays;
	}

	public void setDisplays(String displays) {
		this.displays = displays;
	}



	public String getStyleClasses() {
		return styleClasses;
	}



	public void setStyleClasses(String styleClasses) {
		this.styleClasses = styleClasses;
	}

}
